/**
 * 
 */
package com.roc.web.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.roc.newPage.IPageService;
import com.roc.page.BaseSearchCondition;
import com.roc.page.PaginationBaseAction;
import com.roc.pojo.Operators;
import com.roc.pojo.SysLog;
import com.roc.service.IMerchantServices;
import com.roc.service.IPagenitionService;
import com.roc.service.ServiceException;
import com.roc.util.Constance;
import com.roc.util.constant.Constant;
import com.roc.util.constant.Role;

/**
 * @author Ren PengFei
 * 
 */
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
@Namespace("/syslog")
public class SysLogAction extends PaginationBaseAction {


	static Logger logger = LogManager.getLogger(SysLogAction.class);

	
	private SysLogSearchCondition searchCondition;
	private List<Operators> merchants = new ArrayList<Operators>();
	@Autowired
	IPageService pagenitionService;
	@Autowired
	IMerchantServices merchantServices;

	@Action(value = "init", results = { 
			@Result(name = "success", location = "/pages/syslog.jsp"),
			@Result(name = "error", location = "/error.jsp"),
            @Result(name="login",location="/pages/syslog.jsp") })
	
	public String init(){
		Operators loginMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		if(!(loginMerchant.getRoleType().equals(Role.SYSTEM.getCode()))){
			 addActionError("无权限访问");
			 return ERROR;
		 }
		
	/*	try {
			
			if(Constance.MERCHANT_TYPE_DAILI.equals(loginMerchant.getRoleType())){
				merchants=merchantServices.listAllSubMerchants(loginMerchant.getId());
			}
			ActionContext.getContext().put("merchantList", merchants);
		} catch (ServiceException e) {
			// TODO Auto-generated catch block
			logger.error(e.getMessage(),e);
			addActionError("代理商基本信息初始化查询失败");
			e.printStackTrace();
			return ERROR;
			
		}*/
		return SUCCESS;
	}
	
	@Action(value = "sysLogSearch", results = { 
			@Result(name = "success", location = "/pages/syslog.jsp"),
			@Result(name = "error", location = "/error.jsp"),
            @Result(name="login",location="/pages/syslog.jsp") })

	public String search(){
		Operators loginMerchant=(Operators)getSession().getAttribute(Constant.SESSION_LOGIN_MERCHANT);
		/*if(loginMerchant.getRoleType().equals(Role.AGENT.getCode()) && !loginMerchant.getAccountNo().equals("sdsyb")){
			 addActionError("无权限访问");
			 return ERROR;
		 }*/
		if(!(loginMerchant.getRoleType().equals(Role.SYSTEM.getCode()) ||loginMerchant.getRoleType().equals(Role.O_MANAGER.getCode()))){
			 addActionError("无权限访问");
			 return ERROR;
		 }
		if(searchCondition==null){
			searchCondition=(SysLogSearchCondition)getSearchConditionFromSession();
			if(searchCondition==null){
				searchCondition=new SysLogSearchCondition();
			}
		}
		try {
			String orgCode=loginMerchant.getOneagentid();
			searchCondition.setOrgCode(orgCode);
			/*if(Constance.MERCHANT_TYPE_DAILI.equals(loginMerchant.getRoleType())){
				searchCondition.setMerchantId(loginMerchant.getId());
		        //init();
				if("99".equals(searchCondition.getOperators())){
					String[] merAccounts=new String[merchants.size()];
					for(int i=0;i<merchants.size();i++){
						merAccounts[i]=merchants.get(i).getAccountNo();
					}
					searchCondition.setOperators_array(merAccounts);
				}
			}*/
			//this.setCurrentPage(this.checkCurrentPage());
			int rows=pagenitionService.count(searchCondition);
			searchRsList=pagenitionService.findAll(searchCondition, calcuStart(getCurrentPage()), Constance.PAGE_MAX_SIZE);
			dealWithTheListSpecially(searchRsList);
			this.setTotalPage(this.calcuTotalPage(rows));
			getRequest().setAttribute(SEARCH_RS_LIST, searchRsList);
			getRequest().setAttribute(CURRENT_PAGE, this.getCurrentPage());
			getRequest().setAttribute(TOTAOL_PAGE, this.getTotalPage());
			setSearchToConditionSession(searchCondition);
		
		} catch (ServiceException e) {
			
			logger.error(e.getMessage(),e);
			addActionError("操作日志查询失败");
			
		}
		return SUCCESS;
	}
	
	@Override
	protected void dealWithTheListSpecially(List<Object> list) {
		// TODO Auto-generated method stub
		super.dealWithTheListSpecially(list);
		List<Object> tempList=new ArrayList<Object>();
		for(Object obj:list){
			SysLog bean=(SysLog)obj;
			bean.setDate(Constance.formateDate(bean.getDtCreate(),"yyyy-MM-dd HH:mm:ss"));
			tempList.add(bean);
		}
		searchRsList.clear();
		searchRsList=tempList;
	}
	@Override
	protected String getInitPageSize() {
		// TODO Auto-generated method stub
		return Constance.PAGE_MAX_SIZE+"";
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSession() {
		// TODO Auto-generated method stub
		return (SysLogSearchCondition)getSession().getAttribute(SEARCH_SESSION_BEAN_SYS_LOG);
	}

	@Override
	protected BaseSearchCondition getSearchConditionFromSubClass()
			throws Exception {
		// TODO Auto-generated method stub
		return searchCondition;
	}

	@Override
	protected void setSearchConditionToSubClass(
			BaseSearchCondition searchCondition) throws Exception {
		// TODO Auto-generated method stub
		this.setSearchConditionToSubClass(searchCondition);
	}

	@Override
	protected void setSearchToConditionSession(
			BaseSearchCondition searchCondition) {
		// TODO Auto-generated method stub
		getSession().setAttribute(SEARCH_SESSION_BEAN_SYS_LOG, searchCondition);
	}

	/**
	 * @return the searchCondition
	 */
	public SysLogSearchCondition getSearchCondition() {
		return searchCondition;
	}

	/**
	 * @param searchCondition the searchCondition to set
	 */
	public void setSearchCondition(SysLogSearchCondition searchCondition) {
		this.searchCondition = searchCondition;
	}


}
